/**
 * 原理实现
 * 1.调用首页的内容初始值为path 为 1
 * 2.先获取整个页面的内容然后筛选出 /home/...的 然后存入连接根一起存数组里面去
 *3.然后依次打开上面的网页再一次查.jpg结尾的然后分流下载下来保存到相应文文件中
 * 4.然后就让这个id自增加1再一次调用上面这个函数从头再来一次
 * 5.这是结束时候写的，做了很多的改动！
 * @param url
 * @returns {Promise<unknown>}
 */
//  https://www.e99swwuv4125s266.com/home/pic/11/828_1717010.html
var xuyaoshu = 100;//这是在定义你需要多少张图片
var jishuon = 1;
var id = 1; //当前的页数可以直接跳过
var jinruzhangtai = true; //执行的状态
var tupiansuu = 0;
var zhuangtai = false;
var tupianpath = "https://pic1.hmpicimage.com/jingping";
var gen = "https://www.e99swwuv4125s266.com"
var zhuyepath = "https://www.e99swwuv4125s266.com/home/piclist/11/828-"+id+".html";
var lingshi = "";
const fs = require("fs");
var http = require('https');
var request = require('request');
function loadPage(url) {

    var pm = new Promise(function (resolve, reject) {
        http.get(url, function (res) {
            var html = "https://www.e99swwuv4125s266.com/home/piclist/11/828-"+id+".html";
            res.on('data', function (d) {
                html  = html+ d.toString()
            });
            res.on('end', function () {
                resolve(html);
            });
        }).on('error', function (e) {
            reject(e)
        });
    });
    return pm;
}

function loadPage01(url) {
    var res = new Promise(function (resolve, reject) {
        http.get(url, function (res) {
            var html = lingshi;
            res.on('data', function (d) {
                html  = html+ d.toString()
            });
            res.on('end', function () {
                resolve(html);
            });
        }).on('error', function (e) {
            reject(e)
        });
    });
    return res;
}

function openw(url) {
    if(tupiansuu >xuyaoshu) {
        return;
    }
    jinruzhangtai = false;
    loadPage01(url).then(function (d) {
        //console.log(d)
       // var zx = "fdgfhttps://pic1.hmpicimage.com/jingping/2021/05/27/64de2667-c0c1-40c0-9a08-12cb6a9783f0/046.jpgfgdgd"
        var regex = /https:\/\/pic1.hmpicimage.com\/jingping\/[0-9]{3,5}\/[0-9]{1,3}\/[0-9]{1,3}\/[0-9 a-z A-Z,-]{25,40}\/[0-9]{2,4}.jpg/g;
        var cc1 =  d.toString().match(regex)
        //console.log(cc1)
        var foenumberindex = 0
        for(var i=0;i<cc1.length;i++){
            tupiansuu++;
            console.log(cc1[i]);
                let imgUrl = cc1[i];
                let filename = `test${tupiansuu}.jpg`;
               //request(imgUrl).pipe(fs.createWriteStream("./image/" + filename));
            let fileStream = fs.createWriteStream("./image/" + filename,{autoClose:true})
           request(imgUrl).pipe(fileStream);
            }
        setTimeout(function () {
            jinruzhangtai = true;
        },5000)
        console.log("完成！")
        // if(zhuangtai){
        //     id++;
        //     text01();
        // }
    });
}
function  text01(){
    console.log("这是第一"+id+"个页面的图")
    loadPage("https://www.e99swwuv4125s266.com/home/piclist/11/828-"+id+".html").then(function (d) {
        // console.log(d);
        //home/pic/11/828_1717037.html
        //home/pic/11/828_1717008.html
      // var aa = "gtrgy/home/pic/11/828_1717008.html4564564564"
        var regex = /\/home\/pic\/11\/828_\d{6,8}.html/g;
        var cc =   d.toString().match(regex)

        var ccchnagdu = cc.length;

             var jishishiqide =   setInterval(function () {

                console.log("在第" + id + "页面的，位置是：" + jishuon + "个");
                if (ccchnagdu-1 == jishuon) {
                    jishuon = 1;
                    id++;
                    text01();
                }
                while (jinruzhangtai) {
                    if(tupiansuu <xuyaoshu) {
                    jishuon++;
                    var zcwy = gen + cc[jishuon]
                    lingshi = zcwy;
                    openw(zcwy);
                }else {
                       + console.log("你设置缓存的文件是: " + xuyaoshu+"已经为你缓存了"+tupiansuu+"张美女图片+按Ctrl + c停止！")
                        clearInterval(jishishiqide)
                        return;
                    }
                }
            }, 1000)

    });

}
text01();
//openw("https://www.e99swwuv4125s266.com/home/pic/11/828_1717026.html")